<ix-modal-header
  [requiredRoles]="requiredRoles"
  [title]="'LDAP' | translate"
  [loading]="isLoading"
></ix-modal-header>

<mat-card>
  <mat-card-content>
    <form class="ix-form-container" [formGroup]="form" (submit)="onSubmit()">
      <div class="columns">
        <ix-fieldset>
          <ix-chips
            formControlName="hostname"
            [label]="'Hostname' | translate"
            [tooltip]="helptext.hostnameTooltip | translate"
            [required]="!!(isEnabled$ | async)"
          ></ix-chips>

          <ix-input
            formControlName="basedn"
            [label]="'Base DN' | translate"
            [tooltip]="helptext.basednTooltip | translate"
          ></ix-input>

          <ix-input
            formControlName="binddn"
            [label]="'Bind DN' | translate"
            [tooltip]="helptext.binddnTooltip | translate"
          ></ix-input>

          <ix-input
            formControlName="bindpw"
            type="password"
            [label]="'Bind Password' | translate"
            [tooltip]="helptext.bindpwTooltip | translate"
          ></ix-input>

          <ix-checkbox
            formControlName="enable"
            [label]="'Enable' | translate"
          ></ix-checkbox>

          @if (isAdvancedMode) {
            <ix-checkbox
              formControlName="anonbind"
              [label]="'Allow Anonymous Binding' | translate"
              [tooltip]="helptext.anonbindTooltip | translate"
            ></ix-checkbox>

            <ix-select
              formControlName="ssl"
              [required]="true"
              [label]="'Encryption Mode' | translate"
              [tooltip]="helptext.sslTooltip | translate"
              [options]="sslOptions$"
            ></ix-select>

            <ix-with-manage-certificates-link>
              <ix-select
                formControlName="certificate"
                [label]="'Certificate' | translate"
                [tooltip]="helptext.certificateTooltip | translate"
                [options]="certificates$"
              ></ix-select>
            </ix-with-manage-certificates-link>
            <ix-checkbox
              formControlName="validate_certificates"
              [label]="'Validate Certificates' | translate"
              [tooltip]="helptext.validateCertificatesTooltip | translate"
            ></ix-checkbox>

            <ix-checkbox
              formControlName="disable_freenas_cache"
              [label]="'Disable LDAP User/Group Cache' | translate"
              [tooltip]="helptext.disableCacheTooltip | translate"
            ></ix-checkbox>
          }
        </ix-fieldset>
        @if (isAdvancedMode) {
          <ix-fieldset>
            <ix-select
              formControlName="kerberos_realm"
              [label]="'Kerberos Realm' | translate"
              [tooltip]="helptext.kerberosRealmTooltip | translate"
              [options]="kerberosRealms$"
            ></ix-select>

            <ix-select
              formControlName="kerberos_principal"
              [label]="'Kerberos Principal' | translate"
              [tooltip]="helptext.kerberosPrincipalTooltip | translate"
              [options]="kerberosPrincipals$"
            ></ix-select>

            <ix-input
              formControlName="timeout"
              type="number"
              [label]="'LDAP Timeout' | translate"
              [tooltip]="helptext.timeoutTooltip | translate"
            ></ix-input>

            <ix-input
              formControlName="dns_timeout"
              type="number"
              [label]="'DNS Timeout' | translate"
              [tooltip]="helptext.dnsTimeoutTooltip | translate"
            ></ix-input>

            <ix-textarea
              formControlName="auxiliary_parameters"
              [label]="'Auxiliary Parameters' | translate"
              [tooltip]="helptext.auxiliaryParametersTooltip | translate"
            ></ix-textarea>

            <ix-select
              formControlName="schema"
              [label]="'Schema' | translate"
              [tooltip]="helptext.schemaTooltip | translate"
              [options]="schemaOptions$"
            ></ix-select>
          </ix-fieldset>
        }
      </div>

      <ix-form-actions>
        <button
          *ixRequiresRoles="requiredRoles"
          mat-button
          type="submit"
          color="primary"
          ixTest="save"
          [disabled]="form.invalid"
        >
          {{ 'Save' | translate }}
        </button>

        <button mat-button type="button" ixTest="toggle-advanced-options" (click)="onAdvancedModeToggled()">
          {{ isAdvancedMode ? ('Basic Options' | translate) : ('Advanced Options' | translate) }}
        </button>
        <button
          *ixRequiresRoles="requiredRoles"
          mat-button
          type="button"
          ixTest="rebuild-cache"
          (click)="onRebuildCachePressed()"
        >
          {{ 'Rebuild Directory Service Cache' | translate }}
        </button>
      </ix-form-actions>
    </form>
  </mat-card-content>
</mat-card>
